(function (window,$) {

/**
 * 扩展日期格式化函数
 * @param fmt 日期样式
 */
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
/*以formdata 提交数据
  不启用缓存

*/
$.ajaxSetup({
    dataType: "json",
    contentType: "application/x-www-form-urlencoded",
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    error: function (jqXHR, textStatus, errorThrown) {
        switch (jqXHR.status) {
            case (500):
                alert('服务器繁忙，请稍后！');
                break;
            case (401):
                alert('未登录');
                break;
            case (403):
                alert('无权限执行此操作');
                break;
            case (408):
                alert('请求超时');
                break;
            default:
                alert('未知错误,请联系管理员');
        }
    },
    cache: false
});
//全局命名控件
var frame = window.Frame2 = {};
frame.alert = {
    $div : null,
    init: function (config) {
        var _ = this;
        _.$div = config.$div.find('.alert');
        _.$div.find('button.close').click(function(){
            _.hide(100);
        })
        return _;
    },
    show: function (speed, callback, config) {
        var _ = this;
        config = $.extend(true, {title:'提示：',content:'操作成功。'}, config);

        _.$div.find('.title').text(config.title);
        _.$div.find('.content').text(config.content);
        _.$div.fadeIn(speed, callback);
        if (!!config.autohide && config.autohide > 0) {
            window.setTimeout(function () {
                _.hide(1000)
            },config.autohide)
        };
        return _;
    },
    hide: function (speed, callback) {
        var _ = this;
        _.$div.fadeOut(speed, callback);
        return _;
    },
    fn: function () {
        var _ = this;
        return _;
    }
}// end frame.alert

const _MODEL_CONFIG = {
    model: null,// model容器
    form: null,// 表单容器
    formType: null,// 表单类别: add-新增 update-修改 view-详情
    fields: [],//字段，不包括id
    idField: 'id',//id字段 唯一标识符
}
frame.Model = function (config){
    this.config = $.extend({}, _MODEL_CONFIG, config);
}
frame.Model.prototype = {
    constructor: frame.Model,
    say: function () {
        alert(this.config.name)
    },
    show: function(row) {
        var _ = this;
        if (true) {} else{};
        model.modal('show');
        return _;
    }
}
frame.Model.init = function(config){
    return new frame.Model(config)
}
frame.bootstrap = {
    tableConfig: function (config) {
        var _default = {
            method: 'POST',
            cache:false,
            pagination:true,
            sidePagination : 'server',//服务器端分页 返回数据 {rows:[],total:1}
            pageList: [10, 20, 50, 100],//分页步进值
        };
        return $.extend({}, _default, config);
    },
    getSelectionsAttr: function(table, attr){
        var arr = [];
        var $sel = table.bootstrapTable('getSelections');
        $.each($sel, function(index, val) {
            arr.push(val[attr])
        });
        return arr;
    },
    validatorConfig: function (config) {
        var _default = {
            message: 'This value is not valid',
            excluded: [":disabled"],//无需验证的
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',//显示验证成功或者失败时的一个小图标
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            }
        };
        return $.extend({}, _default, config);
    }
}; //end frame.bootstrap

frame.setViewField = function ($view, select, value) {
    value = value | '';
    $view.find(select).text(value);
}
})(window, jQuery);
